package com.wy.tingshu.servlet.user;

import com.wy.tingshu.service.UserService;
import com.wy.tingshu.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 登录
 **/
@WebServlet("/studio/user/login.do")
public class LoginDoServlet extends HttpServlet {
    private final UserService userService=new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String  username=req.getParameter("username");
        String password=req.getParameter("password");

        //调用UserService中的登陆方法
        UserVO userVO=userService.login(username,password);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        PrintWriter writer= resp.getWriter();
        if(userVO==null){
            writer.println("登陆失败,请重新登录");
            return;
        }else {
            HttpSession session = req.getSession();//登陆成功应先把session写入
            session.setAttribute("currentUser",userVO);
            writer.println("登陆成功");
            //重定向到创作中心的首页
            resp.sendRedirect("/studio/");
        }


    }
}
